package com.lagou.edu.utils;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: PackageScanUtils
 * @Auther: Jerry
 * @Date: 2020/7/7 22:47
 * @Desctiption: 包扫描工具类
 * @Version: 1.0
 */
public class PackageScanUtils {

    private static List<String> nameList = new ArrayList<>();

    /**
     * 扫描包路径下的所有java类
     * @param packageScan 需要扫描的包路径
     * @param excludePackage 需要排除的包路径
     */
    public static List<String> doScanner(String packageScan, String excludePackage) {
        URL resource = PackageScanUtils.class.getClassLoader().getResource(packageScan.replaceAll("\\.", "/"));
        File file = new File(resource.getFile());
        for (File f : file.listFiles()) {
            if (f.isDirectory()) {
                // 递归扫描子包
                doScanner(packageScan + "." + f.getName(), excludePackage);
            } else {
                String className = (packageScan + "." + f.getName()).replace(".class", "");
                // 过滤部分包下的java类
                if (className.contains(excludePackage)){
                    continue;
                }else {
                    nameList.add(className);
                }
            }
        }
        return nameList;
    }
}
